package com.ddf.publics.service.impl;

import com.ddf.publics.dao.GuarantyStyleDao;
import com.ddf.publics.entity.*;
import com.ddf.publics.dao.LooseBidDao;
import com.ddf.publics.service.FundService;
import com.ddf.publics.service.LbBidService;
import com.ddf.publics.service.LooseBidService;
import com.ddf.publics.service.UserInfoService;
import com.ddf.publics.tool.Constant;
import com.ddf.publics.tool.MoneyFund;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (LooseBid)表服务实现类
 *
 * @author norman
 * @since 2020-06-28 09:42:50
 */
@Service
public class LooseBidServiceImpl implements LooseBidService {
    @Resource
    private LooseBidDao looseBidDao;
    @Resource
    private GuarantyStyleDao guarantyStyleDao;
    @Autowired
    private LooseBidService looseBidService;
    @Autowired
    private LbBidService lbBidService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private FundService fundService;

    @Override
    public void repayment(String keyid) {
        //散标id
        String lId = keyid.substring(0, keyid.indexOf(","));

        LooseBid looseBid = looseBidService.queryById(Integer.valueOf(lId));
        List<LbBid> lbBidList = lbBidService.queryAllBylId(looseBid.getId());

        for (LbBid lbBid:lbBidList) {
            UserInfo userInfo = userInfoService.userInfoList(lbBid.getBidUserId());//获得投标人
            Float money = lbBid.getBidMoney();

            System.out.print(userInfo.getRealName() + " 投了 " + money + " 元。");
            Float principal = lbBid.getBidMoney()/Float.valueOf(looseBid.getMonth());
            System.out.print("本金为：" +principal);
            money = money*(looseBid.getApr() * 0.01f) / 12f;///利息

            System.out.print("这个月的利息为 " +money);

            int sumMoney = (int) (money + principal);

            System.out.print("余额加上本息应为 "+ (userInfo.getMoney() + sumMoney));

            UserInfo newUserInfo = new UserInfo();
            newUserInfo.setUfId(userInfo.getUfId());
            newUserInfo.setMoney((userInfo.getMoney() + sumMoney));
            userInfoService.update(newUserInfo);

            Fund fund = new Fund();
            fund.setFundAmunt(money);
            fund.setUId(userInfo.getUId());
            fund.setFundPost(MoneyFund.FUND_PAYMENT);//回款
            fundService.insert(fund);
        }
    }

    @Override
    public void toLooseBid(LoanApplication loan) {
        LooseBid lb = new LooseBid();
        lb.setUId(loan.getUserId());
        lb.setMoney(Float.valueOf(loan.getLMoney()));
        lb.setApr(loan.getInterestRate()-3f);
        lb.setMonth(String.valueOf(loan.getLMonth()));
        lb.setApplyDate(new Date());
        lb.setRiskGrade("S");
        lb.setRefundType("按月还款/等额本息");
        lb.setRefundSource("工资");
        lb.setEducationBackground("大专");
        lb.setMarriage("未婚");
        lb.setStatus(Constant.LOOSEBID_STATUS_THE_GENERATED);
        lb.setHouseProperty("无");
        lb.setCarProduction("无");
        int monthlyProfit = loan.getMonthlyProfit();
        if (monthlyProfit >= 0 && monthlyProfit < 3000) {
            lb.setMonthlyProfit("3k");
        } else if (monthlyProfit >= 3000 && monthlyProfit < 5000) {
            lb.setMonthlyProfit("3k ~ 5k");
        } else if (monthlyProfit >= 5000 && monthlyProfit < 8000) {
            lb.setMonthlyProfit("5k ~ 8k");
        } else if (monthlyProfit >= 8000 && monthlyProfit < 15000) {
            lb.setMonthlyProfit("8k ~ 15k");
        } else if (monthlyProfit >= 15000 && monthlyProfit < 30000) {
            lb.setMonthlyProfit("15k ~ 30k");
        } else if (monthlyProfit >= 8000) {
            lb.setMonthlyProfit("30k 以上");
        }
        GuarantyStyle guarantyStyle = this.guarantyStyleDao.queryByLPId(loan.getLId());

        if (guarantyStyle != null) {
            if ("房屋抵押".equals(guarantyStyle.getName())) {
                lb.setHouseProperty("有");
            }

            if ("车辆抵押".equals(guarantyStyle.getName())) {
                lb.setCarProduction("有");
            }
        }

        //存到数据库
        this.insert(lb);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LooseBid queryById(Integer id) {
        return this.looseBidDao.queryById(id);
    }
    
    /**
     * 查询所有数据
     *
     * @return 对象列表
     */
    @Override
    public List<LooseBid> queryAllList() {
        return this.looseBidDao.queryAllList();
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<LooseBid> queryAllByLimit(int offset, int limit) {
        return this.looseBidDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param looseBid 实例对象
     * @return 实例对象
     */
    @Override
    public LooseBid insert(LooseBid looseBid) {
        this.looseBidDao.insert(looseBid);
        return looseBid;
    }

    /**
     * 修改数据
     *
     * @param looseBid 实例对象
     * @return 实例对象
     */
    @Override
    public LooseBid update(LooseBid looseBid) {
        this.looseBidDao.update(looseBid);
        return this.queryById(looseBid.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.looseBidDao.deleteById(id) > 0;
    }
}